// System includes
#include <QDebug>
#include <QGLFormat>
#include <QCleanLooksStyle>

// Local includes
#include "yiggraphicsview.h"
#include "graphics/yigcolorkit.h"
#include "gui/yiggraphicsscene.h"

YigGraphicsView::YigGraphicsView(QWidget *parent) :
    QGraphicsView(parent)
{
    /*
    //setAttribute(Qt::WA_TranslucentBackground, false);
    QGLFormat glFormat( QGL::SampleBuffers );
    //glFormat.setSwapInterval( 1 ); // set VSync
    QGLWidget* glWidget = new QGLWidget( glFormat );
    //glWidget->setAttribute(Qt::WA_TranslucentBackground);
    setViewport(glWidget); // Set Anti-Aliasing and OpenGL*/
    //setViewportUpdateMode( QGraphicsView::FullViewportUpdate );
    //setScene(new YigGraphicsScene);
    //setScene(new QGraphicsScene);

    //scene()->setItemIndexMethod(QGraphicsScene::NoIndex);
    //setViewportUpdateMode( QGraphicsView::BoundingRectViewportUpdate );
    //setCacheMode( QGraphicsView::CacheBackground );
    //setScene(new YigGraphicsScene);
    setRenderHints(QPainter::Antialiasing);
    setOptimizationFlag(QGraphicsView::DontAdjustForAntialiasing, true);
    //setOptimizationFlag(QGraphicsView::DontSavePainterState, true);
    setFrameStyle(0);
    show();


}

YigGraphicsView::~YigGraphicsView()
{
    delete selectionRect;
}

/* THIS DOESN'T WORK CORRECTLY
void YigGraphicsView::slotHandleResize(QResizeEvent *event)
{

    if( scene() )
        scene()->setSceneRect( QRect( QPoint( 0, 0 ), event->size() ));
    this->resize(scene()->itemsBoundingRect().size().toSize());
}
*/

void YigGraphicsView::resizeEvent( QResizeEvent *event )
{
    if( scene() )
        scene()->setSceneRect( 0, 0, event->size().width(), event->size().height());
    QGraphicsView::resizeEvent( event );
}

void YigGraphicsView::keyPressEvent( QKeyEvent *event )
{
    QGraphicsView::keyPressEvent( event );
}

void YigGraphicsView::mouseMoveEvent( QMouseEvent *event )
{
    if( event->buttons().testFlag( Qt::LeftButton ) )
    {
        // handle selectionRect
        if( selectionRect->isVisible() )
        {
            QPoint mouseDownView = mapFromScene( mouseDownPos );
            lastMouseViewPos = event->pos();
            QRect rubberRect( mouseDownView, lastMouseViewPos );
            rubberRect = rubberRect.normalized();
            selectionRect->setRect( rubberRect );
            QPolygonF p = mapToScene( rubberRect );
            /*
            QPolygonF p;
            p.append(rubberRect.topLeft());
            p.append(rubberRect.topRight());
            p.append(rubberRect.bottomRight());
            p.append(rubberRect.bottomLeft()); */
            QPainterPath path;
            path.addPolygon( p );
            scene()->setSelectionArea( path, Qt::IntersectsItemShape );
        }
    }

    // Always remember to have the parent class handle the event
    QGraphicsView::mouseMoveEvent( event );
}

void YigGraphicsView::mouseReleaseEvent( QMouseEvent *event )
{
    if(selectionRect->isVisible())
    {
        QList<QGraphicsItem*> selectedItems = scene()->selectedItems();
        for(int i = 0; i < selectedItems.size(); i++)
        {
            if(selectedItems.at(i)->type() == YigBaseGraphic::ModFieldType)
            {
                ((YigModField*)selectedItems.at(i))->setSelected(true);
            }
        }
    }
    selectionRect->hide();
    // Always remember to have the parent class handle the event
    QGraphicsView::mouseReleaseEvent( event );
}

void YigGraphicsView::mousePressEvent( QMouseEvent *event )
{
    // Always remember to have the parent class handle the event
    QGraphicsView::mousePressEvent( event );

    if( !scene()->itemAt( mapToScene( event->pos() ) ) )
    {
        mouseDownPos = QGraphicsView::mapToScene( event->pos() ).toPoint();
        lastMouseViewPos = event->pos();
        selectionRect->setRect( mouseDownPos.x(), mouseDownPos.y(), 0, 0 );
        //selectionRect->setRect( mapFromScene( mouseDownPos ).x(), mapFromScene( mouseDownPos ).y(), 0, 0 );
        selectionRect->show();
    }
}


// Zooming worked, but then drag/drop synth locations were off as well as the selection rect.
// perhaps I will find a good solution later
void YigGraphicsView::wheelEvent(QWheelEvent *event)
{

    qreal factor = 1.2;
    if(event->delta() < 0)
    {
        factor = 1.0 / factor;
    }

    scaleFactor *= factor;

    scale(factor, factor);

    QGraphicsView::wheelEvent(event);
}

void YigGraphicsView::paintEvent(QPaintEvent *event)
{
    QPaintEvent* newEvent = new QPaintEvent(event->region().boundingRect());
    QGraphicsView::paintEvent(newEvent);
    delete newEvent;
}
